﻿using LibraryMangementSystem.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMangementSystem.BookRental
{
    public partial class BookRegistration : Form
    {
        public BookRegistration()
        {
            InitializeComponent();
        }

        private void button_loan_Click(object sender, EventArgs e)
        {
            if (textBox_bookId.Text != null && textBox_certId.Text != null && textBox_loneDay.Text != null)
            {
                byte flag = 1;//1 代表已借 0 代表未借
                //textBox_bookId.Text, textBox_certId.Text, dateTimePicker_loandates.Value, dateTimePicker_return.Value, flag
                Loan loan = new Loan();
                loan.BookID = textBox_bookId.Text;
                loan.Cert_id = textBox_certId.Text;
                //loan.Loandates = dateTimePicker_loandates.Value;
                loan.Return = DateTime.Now.AddDays(int.Parse(textBox_loneDay.Text));
                loan.flag = flag;
                //获取当前日期（借书日期）
                DateTime return_date = DateTime.Now;
                loan.Loandates = BookUtil.Format_date(return_date);
                LibraryEntities1 library = new LibraryEntities1();
                //判断此借书证是否能用
                try
                {
                    CertificateInfo certificate = library.CertificateInfo.FirstOrDefault<CertificateInfo>(L => L.Cert_id == loan.Cert_id);
                    if (certificate == null || certificate.flag != 1)
                    {
                        MessageBox.Show("此借书证不能用");
                        return;
                    }
                    //判断是否有此图书
                    BookInfo bookInfo = library.BookInfo.FirstOrDefault<BookInfo>(L => L.BookID == loan.BookID);
                    if (bookInfo == null)
                    {
                        MessageBox.Show("抱歉，没有此图书");
                        return;
                    }


                    int result = 0;
                    String str;

                    //减借书次数
                    int i = BookUtil.reduceCert(loan.Cert_id, library);
                    if (i != 1)
                    {
                        str = "你的借书次数为零，不能借书";
                        MessageBox.Show(str);
                        return;
                    }
                    //先减库存
                    result = BookUtil.reduceStock(bookInfo.BookID, library);
                    if (result != 1)
                    {

                        str = "借书失败,此书库存为0";
                        MessageBox.Show(str);
                        return;

                    }

                    library.Loan.Add(loan);
                    result = library.SaveChanges();
                    str = "借书成功";
                    MessageBox.Show(str);

                }
                catch (System.NullReferenceException ex)
                {

                    MessageBox.Show("没有此借书证，请检查借书证ID是否输入错误！" + ex.Message);
                }
                catch (System.Data.Entity.Infrastructure.DbUpdateException ex)
                {

                    MessageBox.Show("您今天已经借了此书，不能重复借哦！\n" + ex.Message);
                }

            }


        }


        //文本框只允许输入数字 0-9数字  
        private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            }
        }



    }
}
